package com.itheima.web.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter("/*")
public class CharacterEncodingFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        // 处理POST请求乱码问题
        HttpServletRequest request = (HttpServletRequest) req;
        String method = request.getMethod();
        if ("POST".equals(method)) {
            request.setCharacterEncoding("UTF-8");
        }

        chain.doFilter(req, resp); // 放行
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
